#
# POP-C++
# Common (pseudo-dynamic) library build process
#
# AUTHOR: Valentin Clement
# DATE: 2012/11/14
#

set(POPCPP_PSEUDODYNAMIC_INCLUDE_PATH "-I${CMAKE_SOURCE_DIR}/include/pseudodynamic;-I${CMAKE_SOURCE_DIR}/include/core")
# include_directories(${CMAKE_SOURCE_DIR}/ ${CMAKE_SOURCE_DIR}/include/pseudodynamic ${MPI_CXX_INCLUDE_PATH})

# include(CMakeForceCompiler)

set(CMAKE_CXX_OUTPUT_EXTENSION ".o")

# Activate some warnings
add_definitions(-Wall -Wextra)

# Compile in C++11 mode
add_definitions(-std=c++11)

# Indicate that we are compiling for pseudo dynamic
add_definitions(-DPOP_PSEUDO)

add_library(popc_common_psdyn
    broker_receive.cc
    buffer_raw.cc
    buffer_xdr.cc
    interface.cc
    object.cc
    combox_mpi.cc
    connection_mpi.cpp
    system.cc
    utils.cc
)

# SET (POPCCOMMON -Wl,-ldl -Wl,-lpthread)

# TARGET_LINK_LIBRARIES(popc_common_psdyn ${POPCCOMMON})

add_custom_command(
    OUTPUT infmain.psdyn.o
    COMMAND ${MPI_CXX_COMPILER} ${POPC_MAIN_FLAGS} infmain.psdyn.cc
    ${POPCPP_PSEUDODYNAMIC_INCLUDE_PATH}
    DEPENDS infmain.psdyn.cc
    COMMENT "Compile POP-C++ Master main")

add_custom_command(
    OUTPUT objmain.psdyn.o
    COMMAND ${MPI_CXX_COMPILER} ${POPC_MAIN_FLAGS} objmain.psdyn.cc
    ${POPCPP_PSEUDODYNAMIC_INCLUDE_PATH}
    DEPENDS objmain.psdyn.cc
    COMMENT "Compile POP-C++ Object main")

add_custom_target(popcpp_object_psdyn_main ALL DEPENDS objmain.psdyn.o)
add_custom_target(popcpp_master_psdyn_main ALL DEPENDS infmain.psdyn.o)

# Final link steps

# link manually all files generated by popc and add them to static lib
add_custom_command(
    OUTPUT libpopc_common_psdyn.a
    COMMAND ar r libpopc_common_psdyn.a ${POPCC_GENERATED_FILES}
    DEPENDS ${POPCC_GENERATED_FILES}
    COMMENT "Compile POP-C++ generated lib for popc_common_psdyn")

# Install all the files

install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/infmain.psdyn.o DESTINATION lib/pseudodynamic)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/objmain.psdyn.o DESTINATION lib/pseudodynamic)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/libpopc_common_psdyn.a DESTINATION lib/pseudodynamic)
